package array;

/**
 * 给你两个二进制字符串 a 和 b ，以二进制字符串的形式返回它们的和。
 * 示例 1：
 *
 * 输入:a = "11", b = "1"
 * 输出："100"
 * 示例 2：
 *
 * 输入：a = "1010", b = "1011"
 * 输出："10101"
 *
 */

public class L67 {
    public String addBinary(String a, String b) {
       /* StringBuilder stringBuilder = new StringBuilder();
        int i = a.length() - 1;
        int j = b.length() - 1;
        int carry = 0;
        while (i >= 0 || j >= 0 || carry != 0) {
            if (i >= 0) {
                carry=carry + a.charAt(i--)-'0';
            }
            if (j >= 0) {
                carry=carry + b.charAt(j--)-'0';
            }
            stringBuilder.append(carry%2);
            carry/=2;
        }
        return stringBuilder.reverse().toString();*/

        int l = Integer.parseInt(a, 2);
        int r = Integer.parseInt(b, 2);
        int sum = l + r;
        String binaryString = Integer.toBinaryString(sum);
        return binaryString;


    }
    public static void main(String[] args) {
        L67 solution = new L67();
        System.out.println(solution.addBinary("11", "1")); // 输出: "100"
        System.out.println(solution.addBinary("1010", "1011")); // 输出: "10101"
    }
}
